<?php

namespace app\teacher\controller\say;

use app\common\controller\Frontend;
use app\common\enums\AccountTypeEnum;
use app\common\model\banxi\learn\LearnSayReviewRecord;
use app\common\model\banxi\learn\LearnSayReviewSentenceRecord;
use app\common\model\banxi\learn\LearnSkRecord;
use app\common\model\banxi\organization\Organization;
use app\common\service\learn\LearnSkRecordService;
use ba\Exception;
use think\db\exception\PDOException;
use think\exception\ValidateException;

class Review extends Frontend
{
    protected array $noNeedLogin = [];

    protected array $noNeedPermission = ['*'];

    public function initialize(): void
    {
        parent::initialize();
    }

    public function listReviewData()
    {
        $param = $this->request->param(['index', 'limit', 'orderBy', "query", 'studentId']);
        $user = $this->auth->getUserInfo();
        if (!empty($param['studentId'])) {
            $param['query']['EQ_studentId'] = $param['studentId'];
        } else {
            $param['query']['EQ_trainerId'] = $user['id'];
        }
        $param['query']['EQ_isSubmit'] = 1;
        //查询条件解析
        $where = get_query($param);
        //获取分页数据
        $pageParams = get_page_params($param);

        $LearnSayVocabularyRecordModel = new LearnSayReviewRecord();
        $list = $LearnSayVocabularyRecordModel->where($where)
            ->order(['id' => 'desc'])
            ->field('id,answerStartTime,rightNum,errorNum,useTime')
            ->paginate([
                'page' => $pageParams['page'],
                'list_rows' => $pageParams['limit']
            ]);
        $this->ok('ok', result_row($list));
    }

    //开始复习
    public function startReview()
    {
        $skRecordId = $this->request->post('skRecordId', null);
        $reviewType = $this->request->post('type', 1);
        $trainerId = $this->auth->id;
        $studentId = null;
        // 如果提供了上课记录ID
        if ($skRecordId) {
            // 查询上课记录
            $learnSkRecord = LearnSkRecord::where('id', $skRecordId)->find();
            // 验证上课记录是否存在
            if (empty($learnSkRecord)) {
                $this->fail('上课记录不存在');
            }
            // 验证教练ID是否匹配
            if ($learnSkRecord['trainerId'] != $trainerId) {
                $this->fail('上课记录不存在');
            }
            // 获取学生ID
            $studentId = $learnSkRecord['studentId'];
        }
        $LearnSkRecordService = new LearnSkRecordService();
        $result = $LearnSkRecordService->startSayReview($skRecordId, $trainerId, $studentId, $reviewType);
        if (!$result) {
            $this->fail($LearnSkRecordService->getError());
        }
        $this->ok('ok', $result);
    }

    /*
    * 提交答案
   */
    public function answerReview()
    {
        $recordId = $this->request->post('recordId');
        $reviewList = $this->request->filter('')->post('reviewList');
        if (!$recordId) {
            $this->fail('请选择上课记录');
        }
        if (!$reviewList) {
            $this->fail('提交答案不能为空');
        }
        $trainerId = $this->auth->id;
        // 查询上课记录
        $learnSkReviewRecord = LearnSayReviewRecord::where('id', $recordId)->find();
        // 验证上课记录是否存在
        if (empty($learnSkReviewRecord)) {
            $this->fail('上课记录不存在');
        }
        // 验证教练ID是否匹配
        if ($learnSkReviewRecord['trainerId'] != $trainerId) {
            $this->fail('上课记录不存在');
        }
        // 获取学生ID
        $studentId = $learnSkReviewRecord['studentId'];
        $trainerId = $this->auth->id;
        $LearnSkRecordService = new LearnSkRecordService();
        $result = $LearnSkRecordService->answerSayReview($recordId, $trainerId, $studentId, $reviewList);
        if (!$result) {
            $this->fail($LearnSkRecordService->getError());
        }
        $this->ok('ok', $result);
    }

    //获取复习报告
    public function getSayReviewReport()
    {
        $reviewRecordId = $this->request->get('reviewRecordId');
        if (!$reviewRecordId) {
            $this->fail('请选择复习记录');
        }
        try {
            $LearnSayReviewRecordModel = new LearnSayReviewRecord();
            //学习记录数据
            $result = $LearnSayReviewRecordModel->getReviewRecordReport($reviewRecordId, AccountTypeEnum::STUDENT);
            $this->ok('ok', $result);
        } catch (Exception|PDOException|ValidateException $e) {
            $this->fail($e->getMessage());
        }
    }

}